package br.com.iswe.gwt.crud.client;

import br.com.iswe.gwt.ui.client.img.ISWEImages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class PaginationPanel extends Composite {
	
	protected Image separador;
	protected Image primeiraPagina;
	protected Image anterior;
	protected Image proxima;
	protected Image ultimaPagina;
	
	protected int pageTotal 	= -1;
	protected int pageNumber  	= -1;
	protected int pageSize 	    = -1;
	
	protected ListPanelAbstract listPanel;
	
	protected static final ISWEImages isweImages = (ISWEImages)GWT.create(ISWEImages.class);
	
	protected FlexTable flexTable;
	
	public PaginationPanel(ListPanelAbstract listPanel) {
		this();
		this.listPanel = listPanel;
	}
	
	public PaginationPanel() {
		flexTable = new FlexTable();
		initWidget(flexTable);
		
		separador 		= isweImages.separador02().createImage();
		primeiraPagina	= isweImages.botaoInicio().createImage();
		anterior		= isweImages.botaoAnterior().createImage();
		proxima			= isweImages.botaoProximo().createImage();
		ultimaPagina	= isweImages.botaoUltimo().createImage();
		
		flexTable.setCellSpacing(6);
		flexTable.setWidth("100%");
		flexTable.setStyleName("paginacaoPanel");
		
		flexTable.setWidget(0, 0, new Label(" "));
		
		flexTable.setWidget(0, 1, primeiraPagina);
		flexTable.setWidget(0, 2, anterior);
		flexTable.setWidget(0, 3, separador);
		flexTable.setWidget(0, 4, proxima);
		flexTable.setWidget(0, 5, ultimaPagina);
		
		//configura a tabela
		flexTable.getFlexCellFormatter().setWidth(0, 0, "50%");
		
		flexTable.getFlexCellFormatter().setAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getFlexCellFormatter().setWidth(0, 1, "10%");
				
		flexTable.getFlexCellFormatter().setAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getFlexCellFormatter().setWidth(0, 2, "10%");
		
		flexTable.getFlexCellFormatter().setAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getFlexCellFormatter().setWidth(0, 3, "10%");
		
		flexTable.getFlexCellFormatter().setAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getFlexCellFormatter().setWidth(0, 4, "10%");
		
		flexTable.getFlexCellFormatter().setAlignment(0, 5, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getFlexCellFormatter().setWidth(0, 5, "10%");
		
		
		setupScreen();
		
	}
	
	public void reset() {
		pageTotal 		= -1;
		pageNumber  	= -1;
		pageSize 		= -1;
	}
	
	protected void setupScreen() {
		//configura os listeners
		primeiraPagina.addClickHandler(primeiraPaginaListener);
		anterior.addClickHandler(anteriorListener);
		proxima.addClickHandler(proximaListener);
		ultimaPagina.addClickHandler(ultimaPaginaListener);
	}

	
	//////////////////////////////////////////////
	//// Handlers - Listeners 
	//////////////////////////////////////////////
	ClickHandler primeiraPaginaListener = new ClickHandler(){
		public void onClick(ClickEvent event) {
			if(pageNumber > 1){
				pageNumber = 1;
				listPanel.gotoPage(pageNumber, pageSize);
			}
		}		
	};
	ClickHandler anteriorListener = new ClickHandler(){
		public void onClick(ClickEvent event) {
			if(pageNumber > 1){
				listPanel.gotoPage(--pageNumber, pageSize);
			}
		}		
	};
	ClickHandler proximaListener = new ClickHandler(){
		public void onClick(ClickEvent event) {
			//if(pageNumber < pageTotal || pageTotal == -1){
			//	pageNumber = 1;
			//}
			listPanel.gotoPage(++pageNumber, pageSize);
		}		
	};
	ClickHandler ultimaPaginaListener = new ClickHandler(){
		public void onClick(ClickEvent event) {
			if(pageTotal != -1){
				pageNumber = pageTotal;
				listPanel.gotoPage(pageNumber, pageSize);
			}
			else{
				proximaListener.onClick(event);
			}
		}		
	};
	
	public int getPageTotal() {
		return pageTotal;
	}

	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}
	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void notifyPageWithResult(int pageNumber) {
		if(pageTotal < pageNumber){
			pageTotal = pageNumber;
		}
	}
}
